using System;
using System.Windows.Forms;

namespace ScoreCalculator
{
    public partial class Form1 : Form
    {
        bool IntegerValues = true;
        /// <summary>
        /// The number of scores entered
        /// </summary>
        private int count = 0;

        double average;
        /// <summary>
        /// The sum of all the scores entered
        /// </summary>
        private int total = 0;

        /// <summary>
        /// A mark is a rounded score/10
        /// This array stores the number of times a specific mark is obtained
        /// Example: marks[7] gives the number of 7's (scores between 65 and 74)
        /// </summary>
        private int[] marks = new int[11];

        /// <summary>
        /// A string which contains all the entered scores, each on one line
        /// </summary>
        private string allScores = "";

        public Form1()
        {
            for (int i = 0; i < 10; i++)
                marks[i] = 0;
            InitializeComponent();
        }

        private void txtScore_KeyPress(object sender, KeyPressEventArgs e)
        {
          
            
            char ch = e.KeyChar;
            if (!char.IsDigit(ch) && ch!=8 && ch!=13 && ch!=46)
            {
                e.Handled = true;
            }

            if (ch==46)
            {
                IntegerValues = false;
            }


        }

        private void btnAdd_Click(object sender, System.EventArgs e)
        {

            if (txtScore.Text != "")
            {

                if (IntegerValues == true)
                {
                    int score = Convert.ToInt32(txtScore.Text);

                    if (score <= 100)
                    {

                        { // Calculate all necessary values
                            count++;
                            total += score * 2;
                            average = (double)total / (double)count;

                            countMark(score);
                            double percSufficient = (double)100 * ((double)(marks[5] + marks[6] + marks[7] + marks[8] + marks[9] + marks[10]) / (double)count);

                            allScores += score + "\n";

                            // Put the calculated values in the textboxes
                            txtScoreTotal.Text = total.ToString();
                            txtScoreCount.Text = count.ToString();
                            txtAverage.Text = average.ToString();
                            txtPercentageSufficient.Text = percSufficient.ToString();

                            // Clear scores-textbox and give focus
                            txtScore.Clear();
                            txtScore.Focus();
                        }

                    }
                    else
                    {
                        MessageBox.Show("The entered value is bigger than 100\n\t" + score + " > 100");

                        txtScore.Clear();
                        txtScore.Focus();
                    }
                }
                else
                {
                    MessageBox.Show("Please enter integer values only!");
                    txtScore.Clear();
                    txtScore.Focus();
                    IntegerValues = true;
                }
            }
            else
            {
                MessageBox.Show("Please enter values");
                txtScore.Clear();
                txtScore.Focus();
            }
        }

        private void btnDisplay_Click(object sender, System.EventArgs e)
        {
            MessageBox.Show(allScores, "Scores");
            txtScore.Focus();
        }

        private void btnClear_Click(object sender, System.EventArgs e)
        {
            count = 1;
            txtScore.Text = "";
            txtScoreTotal.Text = "";
            txtScoreCount.Text = "";
            txtAverage.Text = "";
            allScores = "";
            txtPercentageSufficient.Text = "";
            txtScore.Focus();
            count = 0;
           
            total = 0;
            for (int i = 0; i < marks.Length; i++)
            {
                marks[i] = 0;
            }
        }

        private void btnExit_Click(object sender, System.EventArgs e)
        {
            Close();
        }

        /// <summary>
        /// Updates array marks according to the current score, 
        /// such that marks contains the number of times each mark occurs
        /// </summary>
        /// <param name="score">The score between 0 and 100</param>
        void countMark(int score)
        {
            for (int i = 1; i < 11; i++)
                if ((10 * i - 5 < score) && (score < 10 * i + 5)) 
                    marks[i-1]++;
        }

        private void txtScoreTotal_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;

            
        }

        private void txtScoreCount_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtAverage_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtPercentageSufficient_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void txtScore_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void txtPercentageSufficient_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtScoreTotal_TextChanged(object sender, EventArgs e)
        {

        }

  
    }
}